/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListFeaturesPresenter.java
 * Date: 08/02/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.EditFeatureEvent;
import com.ingenius.sau.client.event.ListIndicatorsEvent;
import com.ingenius.sau.client.service.GuidelinesServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.guidelines.Factor;
import com.ingenius.sau.shared.model.guidelines.Feature;
import com.ingenius.sau.shared.util.filter.BeanFilter;
import com.ingenius.sau.shared.util.filter.Comparison;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>ListFeaturesPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListFeaturesPresenter implements ListPresenter<Feature> {

	public interface ListFeaturesDisplay extends Display {
		void reload();
	}

	private final HandlerManager eventBus;
	private final GuidelinesServiceAsync rpcService;
	private final ListFeaturesDisplay display;
	private String selectedFeatureId;

	/**
	 * 
	 * @param eventBus
	 * @param rpcService
	 * @param display
	 */
	public ListFeaturesPresenter(HandlerManager eventBus,
			GuidelinesServiceAsync rpcService, ListFeaturesDisplay display) {

		this.eventBus = eventBus;
		this.rpcService = rpcService;
		this.display = display;

		display.setPresenter(this);
		selectedFeatureId = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doAdd()
	 */
	@Override
	public void doAdd() {
		fireEvent(new EditFeatureEvent(null));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doEdit()
	 */
	@Override
	public void doEdit() {
		fireEvent(new EditFeatureEvent(selectedFeatureId));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doDelete()
	 */
	@Override
	public void doDelete() {
		// TODO i18n
		final String failedMessage = "Failed to delete feature";
		final String successMessage = "Feature deleted sucessfully";

		AppController.setStatusMessage("Deleting...");
		rpcService.deleteFeature(selectedFeatureId,
				new AsyncCallback<Boolean>() {
					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_DELETE
								+ Feature.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					@Override
					public void onSuccess(Boolean result) {
						if (result.booleanValue() == true) {
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
						display.reload();
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doInto()
	 */
	@Override
	public void goInto() {
		AppController.setAttribute(Constants.CURRENT_FEATURE_ID,
				selectedFeatureId);
		fireEvent(new ListIndicatorsEvent());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.ListPresenter#fireEvent()
	 */
	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#setSelectedItemId
	 * (java.lang.String)
	 */
	@Override
	public void setSelectedItemId(String id) {
		this.selectedFeatureId = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#loadData(java.util
	 * .List, java.lang.Object, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void loadData(List<Filter> filters, Object loadConfig,
			AsyncCallback<PagingLoadResult<Feature>> callback) {

		String currentFactorId = (String) AppController
				.getAttribute(Constants.CURRENT_FACTOR_ID);
		if (currentFactorId != null) {
			filters = (filters == null) ? filters = new ArrayList<Filter>()
					: filters;
			filters.add(new BeanFilter(Comparison.EQUALS, "factor", new Factor(
					currentFactorId)));
		}
		rpcService.findFeatures(filters, (BasePagingLoadConfig) loadConfig,
				callback);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.Presenter#go(com.google.gwt.user
	 * .client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	@Override
	public String getModuleName() {
		return Constants.TOKEN_GUIDELINES;
	}

}
